function ParseDishes(dishJSON)
{
	return {
			disliked_by_curr_user: new Boolean(dishJSON.extras.is_disliked_by_curr_user),
			name: dishJSON.fields.name,
			liked_by_curr_user: new Boolean(dishJSON.extras.is_liked_by_curr_user),
			id: dishJSON.pk,
			okd_by_curr_user: new Boolean(dishJSON.extras.is_okd_by_curr_user),
			thumbnailUrl: dishJSON.extras.get_thumbnail_url, // != null ? dish.extras.get_thumbnail_url : "images/generic.jpg",
			slug: dishJSON.fields.place.slug,
			place: dishJSON.extras.get_place_name,
			tags: dishJSON.fields.tags,
			lat: dishJSON.fields.place.lat,
			lng: dishJSON.fields.place.lng,
			distance: dishJSON.extras.distance, // serialized from an attribute populated by GeoDjango
	};
}

function ParseFeedItem(json)
{
	return {
			count: json.fields.ranking.count,
			id: json.fields.dish.id,
			modifiedDate: json.fields.modified_date,
			name: json.fields.dish.name,
			place: json.fields.dish.place_name,
            rank: json.fields.rank,
			reviewText: json.fields.comment,
			username: json.fields.ranking.username,
			
			done: false,
	};
}

function ParseReviews(reviewJSON)
{
	return {
			dish: reviewJSON.fields.dish.name,
			id: reviewJSON.pk,
			//slug: reviewJSON.fields.place.slug,
			place: reviewJSON.fields.dish.place,
			user: reviewJSON.extras.get_username,
			userProfileIconUrl: reviewJSON.extras.get_user_profile_icon_url,
			datetime: reviewJSON.fields.created_date,
			text: reviewJSON.fields.text,
	};
}

function ParsePhoto(photoJSON)
{
	return {
			thumbURL: photoJSON.extras.get_thumbnail_url,
			url: photoJSON.fields.image,
	};
}


var DishPageView = Backbone.View.extend({
    // events: {
      // //"keypress #new-rank":  "createOnEnter",
      // //"keyup #new-rank":     "showTooltip",
      // //"click .todo-clear a": "clearCompleted",
	  // "click .inline-heading-button": "submit",
    // },

    initialize: function (spec) {
        _.bindAll(this);
		this.el = $("#tag-main-container");
		
		// default config
        this.config = {
            connect: true
        };

        // extend our default config with passed in object attributes
        _.extend(this.config, spec);
			
		this.photoCollection = new PhotoCollection();
		this.photoCollection.view = new PhotoCollectionView({ model: this.photoCollection });
	
		this.feedCollection = new FeedCollection();
		this.feedCollection.view = new FeedView({ model: this.feedCollection });
		//var dishes = _.map(this.config.dishes, ParseDishes);
		
		this.photoCollection.url = "/api/photos/d/" + this.config.dishId;
		this.feedCollection.url = "/api/feed/d/" + this.config.dishId;
		
		this.load({ dishId: this.config.dishId });
				
		return this;
    },
	
	load: function (args) {
		this.el.load("/dish/" + args.dishId);
		
		
		
		var self = this;
		this.photoCollection.fetch({ 
			success: function (collection, response) {
				
				self.feedCollection.fetch({ 
					success: function (collection, response) {
						// both are done
					}
				});
			}
		});
	}
});


var CategoryController = {
    init: function (spec) {
        // default config
        this.config = {
            connect: true
        };

        // extend our default config with passed in object attributes
        _.extend(this.config, spec);
			
		this.dishCollection = new DishCollection();
		this.dishCollection.view = new DishCollectionView({ model: this.dishCollection, isMapsView: 1, shouldDisplayNames: 1 });
	
        this.model = new MainAppModel({
			dishes: this.dishCollection,
        });
        
		this.view = new SearchView({
			model : this.model,
			el : $("#search_form"),
			geocoder : new google.maps.Geocoder(),
			dishCollectionView : this.dishCollection.view
		});
		
		this.view.geocoder = new google.maps.Geocoder();
			
		//var dishes = _.map(this.config.dishes, ParseDishes);
		
		var tagId = $("#tag-id").text();
		this.dishCollection.url = "/api/dishes/t/" + tagId;
		this.dishCollection.fetch();
		
		return this;
    },
};

var MapController = {
    init: function (spec) {
        // default config
        this.config = {
            connect: true
        };

        // extend our default config with passed in object attributes
        _.extend(this.config, spec);
			
		this.dishCollection = new DishCollection();
		this.dishCollection.view = new DishCollectionView({ model: this.dishCollection });
	
        this.model = new MainAppModel({
			dishes: this.dishCollection,
        });
        
		// this.view = new SearchView({
			// model : this.model,
			// el : $("#search_form"),
			// geocoder : new google.maps.Geocoder(),
			// dishCollectionView : this.dishCollection.view
		// });
		
		//this.view.geocoder = new google.maps.Geocoder();
			
		var dishes = _.map(this.config.dishes, ParseDishes);
		
		this.dishCollection.refresh(dishes);
		return this;
    },
};

var SearchController = {
    init: function (spec) {
        // default config
        this.config = {
            connect: true
        };

        // extend our default config with passed in object attributes
        _.extend(this.config, spec);
			
		this.dishCollection = new DishCollection();
		this.dishCollection.view = new DishCollectionView({ model: this.dishCollection, isMapsView: this.config.isMapsView, shouldDisplayNames: this.config.shouldDisplayNames });
	
        this.model = new MainAppModel({
			dishes: this.dishCollection,
        });
        
		this.view = new SearchView({
			model : this.model,
			el : $("#search_form"),
			geocoder : new google.maps.Geocoder(),
			dishCollectionView : this.dishCollection.view
		});
		
		this.view.geocoder = new google.maps.Geocoder();
			
		var dishes = _.map(this.config.dishes, ParseDishes);
		
		this.dishCollection.refresh(dishes);
		return this;
    },
};

var ReviewController = {
    init: function (spec) {
        // default config
        this.config = {
            connect: true
        };

        // extend our default config with passed in object attributes
        _.extend(this.config, spec);
			
		this.dishCollection = new DishCollection();
		this.dishCollection.view = new DishCollectionView({ model: this.dishCollection, isMapsView: this.config.isMapsView });
	
		this.reviewCollection = new ReviewCollection();
		this.reviewCollection.view = new ReviewCollectionView({ model: this.reviewCollection });
		
		if (this.config.reviews)
		{
			var reviews = _.map(this.config.reviews, ParseReviews);		
			this.reviewCollection.refresh(reviews);
		}
		
		if (this.config.dishes)
		{
			var dishes = _.map(this.config.dishes, ParseDishes);		
			this.dishCollection.refresh(dishes);
		}
		
		return this;
    },
};
